<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>学校列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索筛选 -->
        <el-form :inline="true" class="user-search">
            <el-form-item>
                <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
            </el-form-item>
        </el-form>
        <!--列表-->
        <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border
            element-loading-text="拼命加载中" style="width: 100%;">
            <el-table-column align="center" type="index" label="序号" width="60">
            </el-table-column>
            <el-table-column prop="name" label="学校名称" width="300">
            </el-table-column>
            <el-table-column align="center" label="操作" min-width="300">
                <template slot-scope="scope">
                    <el-button size="mini" type='success' @click="addDetail(scope.row)">添加学校内容</el-button>
                    <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="deleteCategory(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 编辑界面 -->
        <el-dialog :title="title" :visible.sync="editFormVisible" width="60%" @click='closeDialog("edit")'>
            <el-form label-width="120px">
                <el-form-item label="学校名称">
                    <el-input size="small" v-model="param.name" placeholder="请输入学校名称"></el-input>
                </el-form-item>
                <el-form-item label="英文名称">
                    <el-input size="small" v-model="param.eflName" placeholder="请输入英文名称"></el-input>
                </el-form-item>
                <el-form-item label="学校简介">
                    <el-input size="small" :rows="2" type="textarea" v-model="param.synopsis"
                        placeholder="请输入学校简介"></el-input>
                </el-form-item>

                <el-form-item label="学校封面图片">
                    <div class="upload-demo" style="display: flex;align-items: center;">
                        <el-upload action="" :http-request="mainImgUpload" :show-file-list="false"
                            list-type="picture-card">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                        <div class="deleImg" v-if="param.img">
                            <img :src="param.img" class="avatar" />
                            <div class="deleUrl">
                                <el-button type="danger" icon="el-icon-delete" circle @click="delImageClick()"></el-button>
                            </div>
                        </div>

                        
                    </div>
                </el-form-item>
                <!-- <el-form-item label="学校封面图片">
                    <div class="upload-demo" style="display: flex;align-items: center;">
                        <el-upload action="" :http-request="mainImgUpload" :show-file-list="false"
                            list-type="picture-card">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                        <div style="position:relative;" v-if="param.img">
                            <img v-if="param.img" :src="param.img"
                            style="margin-left: 16px;height: 140px;width: 140px;border-radius: 8px;">
                            <div class="deleUrl">
                                <el-button type="danger" icon="el-icon-delete" circle @click="delImageClick()"></el-button>
                            </div>
                        </div>
                    </div>
                </el-form-item> -->

                <el-form-item label="标签">
                    <el-input size="small" v-model="param.label" placeholder="请输入标签"></el-input>
                </el-form-item>
                <el-form-item label="课程名称">
                    <el-input size="small" v-model="param.course" placeholder="请输入课程名称"></el-input>
                </el-form-item>


                <el-form-item label="图片集合">
                    <div class="upload-demo"  style="display: flex;">
                        <el-upload action="" :http-request="mainImglstUpload" :show-file-list="false"
                            list-type="picture-card">
                            <i class="el-icon-plus"></i>
                        </el-upload>
                        <ul class="urlLun" v-if="imageLst.length > 0">
                            <li v-for="(item, index) in imageLst" :key="index" style="width: 140px;height: 140px;margin:3px 0 3px 14px;  position:relative">
                                <img :src="item" style="width: 140px;height: 140px;">
                                <el-button class="deleUrl" type="danger" 
                                icon="el-icon-delete" circle @click="DeleUrl(index)"></el-button>
                            </li>
                        </ul>
                    </div>
                </el-form-item>
                <el-form-item label="学校年级">
                    <el-select :multiple="false" v-model="param.gradeIds" placeholder="请选择学校年级" style="width:240px;">
                        <el-option v-for="item in schoolGradeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学校类型">
                    <el-select :multiple="false" v-model="param.typeId" placeholder="请选择学校类型" style="width:240px;">
                        <el-option v-for="item in schoolTypeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="学校网点">
                    <el-select :multiple="false" v-model="param.outletsId" placeholder="请选择学校网点" style="width:240px;">
                        <el-option v-for="item in schoolOutletList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="建校年份">
                    <el-input size="small" v-model="param.year" placeholder="请输入建校年份"></el-input>
                </el-form-item>
                <el-form-item label="在校人数">
                    <el-input size="small" v-model="param.number" placeholder="请输入在校人数"></el-input>
                </el-form-item>
                <el-form-item label="住宿类型">
                    <el-input size="small" v-model="param.lodgingType" placeholder="请输入住宿类型"></el-input>
                </el-form-item>
                <el-form-item label="师生比例">
                    <el-input size="small" v-model="param.stRatio" placeholder="请输入师生比例"></el-input>
                </el-form-item>
                <el-form-item label="中外教比例">
                    <el-input size="small" v-model="param.zyRatio" placeholder="请输入中外教比例"></el-input>
                </el-form-item>

                <el-form-item label="年度费用">
                    <el-input size="small" v-model="param.fees" placeholder="请输入年度费用"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input size="small" v-model="param.address" placeholder="请输入地址"></el-input>
                </el-form-item>
                <el-form-item label="电话">
                    <el-input size="small" v-model="param.tel" placeholder="请输入电话"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
                <el-button size="small" type="primary" :loading="loading" class="title" @click="addCategory()"
                    v-if="title == '添加'">添加</el-button>
                <el-button size="small" type="primary" :loading="loading" class="title" @click="upCategory()"
                    v-if="title == '修改'">修改</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    addSchoolGrade, selectSchoolAlls,
    selectSchoolTypeAlls,
    selectSchoolGradeAlls,
    selectOutletsAlls,
    addSchool,
    updateSchool,
    deleteSchoolByIds
} from "../../api/userMG";
import client from "@/utils/ossClient.js";
import Pagination from "../../components/Pagination";
export default {
    data() {
        return {
            imageLst: ['http://yytc-img.oss-cn-qingdao.aliyuncs.com/1712744754412.jpg'],
            loading: false, //是显示加载
            editFormVisible: false, //控制编辑页面显示与隐藏
            menuAccessshow: false, //控制数据权限显示与隐藏
            title: "添加",
            name: "",
            sort_order: "",
            // 删除
            seletedata: {
                ids: "",
                token: localStorage.getItem("logintoken"),
            },
            listData: [], //用户数据
            // 选中
            checkmenu: [],
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 10,
                total: 10,
            },
            param: {
                name: '上海交大附中',//学院名字
                eflName: 'SHANGHAI DTD',//学院英文名字
                typeId: '',//学院类型值
                synopsis: '学校非常大 非常牛逼',//学院简介
                img: 'http://yytc-img.oss-cn-qingdao.aliyuncs.com/1712744545192.jpeg',//展示图片
                label: '国际',//标签
                course: '课程名称',//课程名称
                fees: '12万元',//年度费用
                latitude: '12.22',//纬度
                longitude: '168.123',//经度
                imglist: '',//图片集合
                address: '深圳市南山区一栋一单元',//地址
                tel: '17608453491',//电话
                outletsId: '',//网点表id
                gradeIds: '',//年级
                gradeName: '',
                year: '',
                number: '',
                lodgingType: '',
                stRatio: '',
                zyRatio: '',
            },
            schoolTypeList: [],
            schoolGradeList:[],
            schoolOutletList: [],



        };
    },
    // 注册组件
    components: {
        Pagination,
    },
    /**
     * 数据发生改变
     */

    watch: {},

    /**
     * 创建完毕
     */

    created() {
        this.getdata()
        this.schoolTypeLst()//学校类型
        this.schoolGradeLst()//学校年纪
        this.schoolOutletLst()//学校网点
    },
    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
        delImageClick() {
            this.param.img = ''
        },
        addDetail(item) {
            localStorage.setItem('schoolItem', JSON.stringify(item))
            this.$router.push({ path: '/school/schoolDetail' });
        },
        schoolTypeLst() {
            selectSchoolTypeAlls().then(res => {
                if (res.code == 200) {
                    this.schoolTypeList = res.data.data
                }
            })
        },
        schoolGradeLst() {
            selectSchoolGradeAlls().then(res => {
                if (res.code == 200) {
                    this.schoolGradeList = res.data.data
                    // console.log('hhhhh',this.schoolGradeList)
                }
            })
        },
        schoolOutletLst() {
            selectOutletsAlls().then(res => {
                if (res.code == 200) {
                    this.schoolOutletList = res.data.data
                }
            })
        },
        mainImglstUpload(file) {
            //判断扩展名
            const tmpcnt = file.file.name.lastIndexOf(".");
            const exname = file.file.name.substring(tmpcnt + 1);
            const names = ["jpg", "jpeg", "webp", "png", "bmp"];
            if (names.indexOf(exname) < 0) {
                this.$message.error("不支持的格式!");
                return;
            }
            const fileName = file.file.uid + "." + exname;
            client
                .put(fileName, file.file)
                .then((res) => {
                    if (res.url) {
                        this.imageLst.push(res.url);
                    } else {
                        this.$message.error("文件上传失败");
                    }
                })
                .catch((err) => { });
        },
        mainImgUpload(file) {
            //判断扩展名
            const tmpcnt = file.file.name.lastIndexOf(".");
            const exname = file.file.name.substring(tmpcnt + 1);
            const names = ["jpg", "jpeg", "webp", "png", "bmp"];
            if (names.indexOf(exname) < 0) {
                this.$message.error("不支持的格式!");
                return;
            }
            const fileName = file.file.uid + "." + exname;
            client
                .put(fileName, file.file)
                .then((res) => {
                    if (res.url) {
                        this.param.img = res.url;
                    } else {
                        this.$message.error("文件上传失败");
                    }
                })
                .catch((err) => { });
        },
        // 添加分类
        addCategory() {
            this.param.imglist = this.imageLst.join(',')
            let gradeArr = this.schoolGradeList.filter(item => {
                return item.id == parseInt(this.param.gradeIds)
            })
            this.param.gradeName = gradeArr[0].name
            this.loading = true;
            addSchool({
                ...this.param
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.editFormVisible = false
                    this.$message({
                        type: "success",
                        message: res.msg,
                    });
                    this.getdata();
                } else {
                    this.$message.error(res.msg);
                }
            });
        },
        // 修改
        upCategory() {
            // console.log('this.param', this.param)
            // this.loading = true;
            this.param.imglist = this.imageLst.join(',')
            let gradeArr = this.schoolGradeList.filter(item => {
                return item.id == this.param.gradeIds
            })
            this.param.gradeName = gradeArr[0].name
           console.log(this.param);
            // return 
            console.log("asdasd",this.param);
            updateSchool({
                ...this.param
                // school:this.param
            }).then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.editFormVisible = false
                    this.$message({
                        type: "success",
                        message: res.msg,
                    });
                    this.getdata();
                } else {
                    this.$message.error(res.msg);
                }
            });
        },

        // 获取数据方法
        getdata() {
            this.loading = true;

            selectSchoolAlls().then((res) => {
                this.loading = false;
                if (res.code == 200) {
                    this.listData = res.data.data;

                    return;
                }
                this.$message({
                    type: "info",
                    message: res.msg,
                });
            });
        },
        // 分页插件事件
        callFather(parm) {
            this.formInline.page = parm.currentPage;
            this.formInline.limit = parm.pageSize;
            this.getdata(parm.currentPage);
        },
        // 删除图片
        DeleUrl(index){
            this.imageLst
            this.imageLst.splice(index,1);
        },
        //显示编辑界面
        handleEdit: function (row) {
            console.log("row", row);
            this.editFormVisible = true;
            if (row != undefined && row != "undefined") {
                this.title = "修改";
                this.param.id = row.id;
                this.param.name = row.name;
                this.param.eflName = row.eflName;
                this.param.typeId = row.typeId;
                this.param.synopsis = row.synopsis;
                this.param.img = row.img;
                this.param.label = row.label;
                this.param.course = row.course;
                this.param.fees = row.fees;
                this.param.latitude = row.latitude;
                this.param.longitude = row.longitude;
                this.imageLst = row.imglist.split(',');
                this.param.address = row.address;
                this.param.tel = row.tel;
                this.param.outletsId = row.outletsId;
                this.param.gradeIds = parseInt(row.gradeIds);
                this.param.gradeName = row.gradeName;
                this.param.year = row.year;
                this.param.number = row.number;
                this.param.lodgingType = row.lodgingType;
                this.param.stRatio = row.stRatio;
                this.param.zyRatio = row.zyRatio;
            } else {
                this.title = "添加";
                this.param.id = "";
                this.param.name = "";
                this.param.eflName = "";
                this.param.typeId = "",
                this.param.synopsis = "";
                this.param.img = "";
                this.param.label = "";
                this.param.course = "";
                this.param.fees = "";
                this.param.latitude = "";
                this.param.longitude = "";
                this.imageLst = [];
                this.param.address = "";
                this.param.tel = "";
                this.param.outletsId = "";
                this.param.gradeIds = "";
                this.param.gradeName = "";
                this.param.year = "";
                this.param.number = "";
                this.param.lodgingType = "";
                this.param.stRatio = "";
                this.param.zyRatio = "";
            }
        },
        // 删除
        deleteCategory(index, row) {
            this.$confirm("确定要删除吗?", "信息", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
            .then(() => {
                deleteSchoolByIds({ id: row.id })
                    .then((res) => {
                        if (res.code == 200) {
                            this.$message({
                                type: "success",
                                message: "已删除！",
                            });
                            this.getdata();
                        } else {
                            this.$message({
                                type: "info",
                                message: res.msg,
                            });
                        }
                    })
                    .catch((err) => {
                        this.loading = false;
                        this.$message.error("删除失败，请稍后再试！");
                    });
            })
            .catch(() => {
                this.$message({
                    type: "info",
                    message: "已取消删除",
                });
            });
        },

        // 关闭编辑、增加弹出框
        closeDialog(dialog) {
            if (dialog == "edit") {
                this.editFormVisible = false;
            } else if (dialog == "perm") {
                this.menuAccessshow = false;
            }
        },
    },
};
</script>

<style scoped>
.user-search {
    margin-top: 20px;
}

.userRole {
    width: 100%;
}

li{
    list-style: none;
}
.urlLun{
    display: flex; 
    flex-wrap: wrap;
    overflow: auto; 
    width: 100%;
    height: 160px;
    padding:0;
    margin:0;
}
.deleUrl{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    /* display: none; */
}
.urlLun li:hover .deleUrl{
  
}
.deleUrl{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
     
}
.deleImg{
    position: relative;
    margin-left: 16px;
}
.avatar {
    width: 140px;
    height: 140px;
 
}
.deleImg:hover .deleUrl{
    
}
</style>